<?php

namespace app\command\services;

use GuzzleHttp\Client;
use think\facade\Log;

class HxApi
{
    private $client;

    public function __construct()
    {
        $this->client = new Client(['verify' => false]);

    }

    private function getConfig(): array
    {


        return [
//            'AppID' => '174826258948',
            'AppID' => '174841847576',
            'AppSecret' => 'xBjPwkQaW0piXkicMvj85iKaVHJExfh4',
//            'url' => 'https://api-test.zhenxinhuixuan.com/open',
            'url' => 'https://b.zhenxinhuixuan.com/open'
        ];
    }

    /**
     * 获取消息
     * @param $type
     * @param $pageLimit
     * @return mixed
     */
    public function getMeg($type = 0, $pageLimit = 1000)
    {
        $url = '/messagepool/getMessagepool';

        $data = [
            'type' => $type,
            'pageLimit' => $pageLimit
        ];

        $res = $this->apiResponse($url, $data);

        return $res;

    }

    /**
     * 删除消息
     * @param $id
     * @return mixed
     */
    public function delMeg($id)
    {
        $url = '/messagepool/removeMessagepool';

        $data = [
            'id' => $id
        ];

        $res = $this->apiResponse($url, $data,'post');

        return $res;

    }


    /**
     * 查询订单详情
     * @param $id
     * @return mixed
     */
    public function orderDetails($order_sn)
    {
        $url = '/order/details';

        $data = [
            'order_sn' => $order_sn
        ];

        $res = $this->apiResponse($url, $data);

        return $res;

    }

    /**
     * 查询选品库中的SpuId列表[GET/products/getSpuIdList]
     * @param $page
     * @param $perPage
     * @return mixed
     */
    public function getSpuIdList($page = 1, $perPage = 100)
    {
        $url = '/products/getSpuIdList';

        $data = [
            'page' => $page,
            'perPage' => $perPage,
        ];

        $res = $this->apiResponse($url, $data);

        return $res;
    }

    /**
     * 查询SPUID商品详情[GET/products/getSpuIdByDetails]
     * @param $spuId
     * @return mixed
     */
    public function getSpuIdByDetails($spuId)
    {
        $url = '/products/getSpuIdByDetails';

        $data = [
            'spu_id' => $spuId,
        ];

        $res = $this->apiResponse($url, $data);

        return $res;
    }

    /**
     * 查询SPUID商品详情[GET/products/getSkuIdInfo]
     * @param $spuId
     * @return mixed
     */
    public function getSkuIdInfo($skuId)
    {
        $url = '/products/getSkuIdInfo';

        $data = [
            'sku_id' => $skuId,
        ];

        $res = $this->apiResponse($url, $data);

        return $res;
    }


    /**
     * 获取分类列表[GET/products/getCategory]
     * @param $param
     * @return mixed
     */
    public function getCategory($param = [])
    {
        $url = '/products/getCategory';
        $data = [
            'parent_id'=>'0',
            'class_id'=>'1343',
            'level'=>'1',

        ];
        if (!empty($param['parent_id'])) $data['parent_id'] = $param['parent_id'];
        if (!empty($param['class_id'])) $data['class_id'] = $param['class_id'];
        if (!empty($param['level'])) $data['level'] = $param['level'];
        $res = $this->apiResponse($url, $data);

        return $res;
    }

    /**
     * 接口基本调用
     * @param $url
     * @param $dataJson
     * @param $http
     * @return mixed
     */
    public function apiResponse($url, $dataJson = [], $http = 'get')
    {
//        dump([
//            'json' => $dataJson,
//        ]);
        $header = [
            'appId' => $this->getConfig()['AppID'],
            'timestamp' => time(),
            'onceString' => rand(1, 99999),
//            'versions' => 'v2',
        ];
//        $dataJson = array_filter($dataJson);
        $header = $header + ['sign' => $this->getSign($dataJson + $header)];
        $dataJson = $dataJson + $header;
        if ($http == 'post') {
            $response = $this->client->post($this->getConfig()['url'] . $url, [
                'json' => $dataJson,
            ]);
        } else {
            $response = $this->client->get($this->getConfig()['url'] . $url, [
                'json' => $dataJson,
            ]);
        }

//        dump([
//            'json' => $dataJson,
//        ]);
        $body = $response->getBody();

        $res = json_decode($body->getContents(), 1);
//        dump($res);
        if ($res['status'] != 'success') {

            Log::info('接口'.$url.'失败返回信息:'.json_encode($res).'-----入参 Params::' . json_encode($dataJson, 1));
            return false;
        }
        Log::info('接口'.$url.'成功返回信息:'.json_encode($res));
        return $res['data'];
    }

    function isEmpty($data){
        if ($data !== '') {
            return $data;
        }
    }


    /**
     * 查询商品价格[GET/products/findSkuSalePrice]
     * @param $skuId
     * @return mixed
     */
    public function findSkuSalePrice($skuId)
    {
        $url = '/products/findSkuSalePrice';

        $data = [
            'sku_id' => $skuId,
        ];

        $res = $this->apiResponse($url, $data);

        return $res;
    }

    /**
     * 运费查询接口[POST/order/postage]
     * @param $sku_list
     * @return mixed
     */
    public function orderPostage($data)
    {
        $url = '/order/postage';

        $res = $this->apiResponse($url, $data,'post');

        return $res;
    }


    /**
     * 下单接口[POST/order/submit]
     * @param $sku_list
     * @return mixed
     */
    public function orderSubmit($data)
    {
        $url = '/order/submit';

        $res = $this->apiResponse($url, $data,'post');

        return $res;
    }


    /**
     * 库存查询接口[GET/products/stock]
     * @param $sku_list
     * @return mixed
     */
    public function stock($data)
    {
        $url = '/products/stock';

        $res = $this->apiResponse($url, $data);

        return $res;
    }

    /**
     * 申请售后接口[POST/service/submit]
     * @param $sku_list
     * @return mixed
     */
    public function serviceSubmit($data)
    {
        $url = '/service/submit';

        $res = $this->apiResponse($url, $data,'post');

        return $res;
    }


    /**
     * 申请售后接口[POST/service/expressSubmit]
     * @param $sku_list
     * @return mixed
     */
    public function expressSubmit($data)
    {
        $url = '/service/expressSubmit';

        $res = $this->apiResponse($url, $data,'post');

        return $res;
    }


    /**
     * 物流信息[GET/comm/getExpress]
     * @param $sku_list
     * @return mixed
     */
    public function expressList($data)
    {
        $url = '/comm/getExpress';

        $res = $this->apiResponse($url, $data);

        return $res;
    }


    /**
     * 生成签名
     * @param $dataJson
     * @return string
     */
    public function getSign($dataJson)
    {
        ksort($dataJson);
        $tmpString = json_encode($dataJson, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES);
        $tmpString = preg_replace('# #','', $tmpString);
        return strtoupper(md5($tmpString . $this->getConfig()['AppSecret']));
    }
}